<?php
/**
 * Created by PhpStorm.
 * User: liuda
 * Date: 2019/3/15
 * Time: 14:27
 */

namespace app\common\service;


use app\common\model\ProductData;
use app\common\model\User;

class DataService
{
    /**
     * 检测是否需要扣量 true需要扣 false不需要扣
     * @author liud
     * @param $channelId
     * @return bool
     */
    public static function checkHasReduce($channelId)
    {
        if (!$channelId) {
            return false;
        }
        //如果扣量比例设置的0就不扣
        $channel = ChannelService::getChannelByChannelId($channelId);
        if (!$channel) {
            return false;
        }
        $reduce = $channel['reduce'];
        if (empty($reduce)) {
            return false;
        }
        //找到一共扣了几个
        $hasReduceCount = self::getReduceCountByChannelId($channelId);

        //计算全部总共应该扣几个
        $count = self::getCountByChannelId($channelId);
        $shouldReduceCount = floor($count * $reduce / 100);
        //如果够扣返回false 不够返回true
        if($hasReduceCount >= $shouldReduceCount){
            return false;
        }
        return true;
    }

    /**
     * 获取当天总共扣量的个数
     * @author liud
     * @param $channelId
     * @return float|string
     */
    public static function getReduceCountByChannelId($channelId)
    {
        $model = new ProductData();
        $beginTime = strtotime(date('Y-m-d 00:00:00', time()));
        $where = [
            ['channel_id', '=', $channelId],
            ['created', 'between', [$beginTime, time()]],
            ['is_reduce', '=', 1],
        ];
        return $model->where($where)->count();
    }

    public static function getCountByChannelId($channelId)
    {
        $model = new ProductData();
        $beginTime = strtotime(date('Y-m-d 00:00:00', time()));
        $where = [
            ['channel_id', '=', $channelId],
            ['created', 'between', [$beginTime, time()]],
        ];
        return $model->where($where)->count();
    }



    /**
     * 检测是否需要扣量 true需要扣 false不需要扣
     * @author liud
     * @param $channelId
     * @return bool
     */
    public static function checkUserHasReduce($channelId)
    {
        if (!$channelId) {
            return false;
        }
        //如果扣量比例设置的0就不扣
        $channel = ChannelService::getChannelByChannelId($channelId);
        if (!$channel) {
            return false;
        }
        $reduce = $channel['reduce'];
        if (empty($reduce)) {
            return false;
        }
        //找到一共扣了几个
        $hasReduceCount = self::getUserReduceCountByChannelId($channelId);
        //计算全部总共应该扣几个
        $count = self::getUserCountByChannelId($channelId);
        $shouldReduceCount = floor($count * $reduce / 100);
        //如果够扣返回false 不够返回true
        if($hasReduceCount >= $shouldReduceCount){
            return false;
        }
        return true;
    }

    /**
     * 获取当天总共扣量的个数
     * @author liud
     * @param $channelId
     * @return float|string
     */
    public static function getUserReduceCountByChannelId($channelId)
    {
        $model = new User();
        $beginTime = strtotime(date('Y-m-d 00:00:00', time()));
        $where = [
            ['channel_id', '=', $channelId],
            ['created', 'between', [$beginTime, time()]],
            ['is_reduce', '=', 1],
        ];
        return $model->where($where)->count();
    }

    public static function getUserCountByChannelId($channelId)
    {
        $model = new User();
        $beginTime = strtotime(date('Y-m-d 00:00:00', time()));
        $where = [
            ['channel_id', '=', $channelId],
            ['created', 'between', [$beginTime, time()]],
        ];
        return $model->where($where)->count();
    }
}